package cn.sh.library.gmwx.preload;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ClassUtils;

@Configuration //@Configuration扫描, 项目启动时扫描该类
public class PreLoadPersonName {
	
	@Bean(name = "preloadPersonInfo") //@Bean注入, 项目启动时执行该方法
	public Map<String, String> putPersonNameByPersonUri() {
		Map<String, String> preloadPersonInfo = new HashMap<String, String>();
		try {
			String txtPath = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"personList.txt";
			InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(txtPath), "utf-8"); 
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			String personList = "";
			// 按行读取字符串
			while ((personList = bufferedReader.readLine()) != null) {
				String [] personUriAndName = personList.split("#");
				String personUri = personUriAndName[0];
				String personName = personUriAndName[1];
				preloadPersonInfo.put(personUri, personName);
			}
			bufferedReader.close();
			inputStreamReader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return preloadPersonInfo;
	}
	
}
